if (SERVER) then
	AddCSLuaFile("shared.lua")
end

if (CLIENT) then
	SWEP.PrintName = "ATM Cracker"
	SWEP.Slot = 4
	SWEP.SlotPos = 1
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = true
	SWEP.IsCracking = false
end

-- Variables that are used on both client and server

SWEP.Author = "Chief Tiger"
SWEP.Instructions = "Left click to crack a keypad"
SWEP.Contact = ""
SWEP.Purpose = ""

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.ViewModel = Model("models/weapons/v_c4.mdl")
SWEP.WorldModel = Model("models/weapons/w_c4.mdl")

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.Sound = Sound("weapons/deagle/deagle-1.wav")

SWEP.Primary.ClipSize = -1      -- Size of a clip
SWEP.Primary.DefaultClip = 0        -- Default number of bullets in a clip
SWEP.Primary.Automatic = false      -- Automatic/Semi Auto
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1        -- Size of a clip
SWEP.Secondary.DefaultClip = -1     -- Default number of bullets in a clip
SWEP.Secondary.Automatic = false        -- Automatic/Semi Auto
SWEP.Secondary.Ammo = ""
SWEP.KeyCrackTime = 40

/*---------------------------------------------------------
Name: SWEP:Initialize()
Desc: Called when the weapon is first loaded
---------------------------------------------------------*/
function SWEP:Initialize()
	if (SERVER) then
		self:SetWeaponHoldType("normal")
	end
end

/*---------------------------------------------------------
Name: SWEP:PrimaryAttack()
Desc: +attack1 has been pressed
---------------------------------------------------------*/

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire(CurTime() + .4)
	if self.IsCracking then if SERVER then GAMEMODE:Notify(self.Owner, 3, 10, math.Round(self.EndCrack- CurTime()) .. " seconds left!") end return end

	if SERVER then GAMEMODE:Notify(self.Owner, 3, 10, self.KeyCrackTime.." seconds till completion!") end

	if math.random(1,10)!=1 then
		if SERVER then GAMEMODE:Notify(self.Owner, 1, 10, "You've set off the silent alarm! Hurry up then get out of there!") end
		for k,v in pairs(player.GetAll()) do
			if SERVER and v:Team()==TEAM_POLICE or v:Team()==TEAM_SWAT or v:Team()==TEAM_CHIEF or v:Team()==TEAM_SWATSPECIAL or v:Team()==TEAM_SWATLEADER then
				if SERVER then GAMEMODE:Notify(v, 3, 10, "WARNING: ATM hacker detected! Go find and arrest them!") end
			end
		end
	end

	if( math.random(1,7) == 4 ) then
		if SERVER then 
			GAMEMODE:Notify(self.Owner, 1, 10, "Ouch, you got electricuted try being more carefull.")
			self.Owner:TakeDamage( math.random(3,7), self.Owner, self.Weapon )
		end
	end

	local trace = self.Owner:GetEyeTrace()
	local e = trace.Entity
	if IsValid(e) and trace.HitPos:Distance(self.Owner:GetShootPos()) <= 300 and (e:GetClass() == "atm_sent") then
		self.IsCracking = true
		self.StartCrack = CurTime()
		self.EndCrack	= CurTime() + self.KeyCrackTime
		if SERVER then
			self:SetWeaponHoldType("pistol")
			timer.Create("KeyCrackSounds", 1, self.KeyCrackTime, function()
				e:EmitSound("buttons/blip2.wav", 100, 100)
			end, self)
		end
		if CLIENT then
			self.Dots = self.Dots or ""
			timer.Create("KeyCrackDots", 0.5, 0, function(wep) 
				if not IsValid(wep) then timer.Destroy("KeyCrackDots") return end
				local len = string.len(wep.Dots)
				local dots = {[0]=".", [1]="..", [2]="...", [3]=""}
				wep.Dots = dots[len]
			end, self)
		end
	end 
end

function SWEP:Holster()
	self.IsCracking = false
	if SERVER then timer.Destroy("KeyCrackSounds") end
	if CLIENT then timer.Destroy("KeyCrackDots") end
	return true
end

function SWEP:Succeed()
	self.IsCracking = false
	if SERVER then 
		timer.Destroy("KeyCrackSounds") 
		local givemoney = math.random(1,5000*math.random(0.1)*math.random(0.1)*math.random(0.1))
		self.Owner:AddMoney(givemoney)
		GAMEMODE:Notify(self.Owner, 4, 4, "You managed to grab $" .. givemoney .. " out of that ATM!")
	end
	if CLIENT then timer.Destroy("KeyCrackDots") end
end

function SWEP:Fail()
	self.IsCracking	= false
	if SERVER then self:SetWeaponHoldType("normal")
	timer.Destroy("KeyCrackSounds") end
	if CLIENT then timer.Destroy("KeyCrackDots") end
end

function SWEP:Think()
	if self.IsCracking then
		local trace = self.Owner:GetEyeTrace()
		if not IsValid(trace.Entity) then 
			self:Fail()
		end
		if trace.HitPos:Distance(self.Owner:GetShootPos()) > 300 or (trace.Entity:GetClass() != "atm_sent") then
			self:Fail()
		end
		if self.EndCrack <= CurTime() then
			self:Succeed()
		end
	end
end

function SWEP:DrawHUD()
	if SERVER then return end
	if self.IsCracking then
		self.Dots = self.Dots or "..."
		local w = ScrW()
		local h = ScrH()
		local x,y,width,height = w/2-w/10, h/3, w/5, h/15
		draw.RoundedBox(0, x, y, width, height, Color(10,10,10,240))
		
		local time = self.EndCrack - self.StartCrack
		local curtime = CurTime() - self.StartCrack
		local status = curtime/time
		local BarWidth = status * (width - 32) + 8
		draw.RoundedBox(0, x+8, y+8, BarWidth, height - 16, Color(255-(status*255), 0+(status*255), 0, 255))
		
		draw.SimpleText("Cracking"..self.Dots, "Trebuchet24", w/2, y+24, Color(255,255,255,255), 1, 1)
	end
end

function SWEP:SecondaryAttack()
	self:PrimaryAttack()
end